package at.fpmedv.eclipse.helma.views;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class HelmaPrototypeOutlineActivator extends AbstractUIPlugin {

	/**
	 *  The plug-in ID
	 */
	public static final String PLUGIN_ID = "at.fpmedv.eclipse.helma.views.PrototypeOutline";

	/**
	 *  The shared instance (Singelton)
	 */
	private static HelmaPrototypeOutlineActivator plugin;
	
	/**
	 * where to find the icons in this package
	 */
	private static String ICON_BASE_URL= "icons/";

	/**
	 * image name of the icon for the refresh action
	 */
	public static final String ICON_REFRESH = "refresh.gif";
	
	/**
	 * image name of the icon for the application tree object
	 */
	public static final String TREE_ICON_APPLICATION = "database.png";
	
	/**
	 * image name of the icon for the code tree object
	 */
	public static final String TREE_ICON_CODE = "page_code.png";

	/**
	 * image name of the icon for the code tree object
	 */
	public static final String TREE_ICON_CORE_CODE = "page_core_code.png";

	/**
	 * image name of the icon for the application skin object
	 */
	public static final String TREE_ICON_SKIN = "page_world.png";
	
	/**
	 * image name of the icon for the prototype tree object
	 */
	public static final String TREE_ICON_PROTOTYPE = "package.png";

	/**
	 * image name of the icon for the Property tree object
	 */
	public static final String TREE_ICON_PROPERTY = "cog.png";

	/**
	 * image name of the icon for the Property tree object
	 */
	public static final String TREE_ICON_OBJECT = "cog.png";

	/**
	 * image name of the icon for the Property tree object
	 */
	public static final String TREE_ICON_COLLECTION = "cogs.png";
	
	/**
	 * image name of the icon for the Mountpoint tree object
	 */
	public static final String TREE_ICON_MOUNTPOINT = "connect.png";
	
	/**
	 * the tree that shows the live outline
	 */
	private TreeViewer viewer;
	
	/**
	 * The constructor does nothing
	 */
	public HelmaPrototypeOutlineActivator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static HelmaPrototypeOutlineActivator getDefault() {
		return plugin;
	}

    /**
     * Creates an ImageDescriptor by loading it from a file in
     * the plugin's images directory.
     *
     * @param imageName The path to the image file to load.
     *     The image path must not include the plugin's images
     *     path location defined in <code>ICON_BASE_URL</code>.
     *     If you want to load <code>ICON_BASE_URL/myimage.gif</code>,
     *     simply pass <code>myimage.gif</code> to this method.
     *
     * @return The image object loaded from the image file
     */
    public static ImageDescriptor getImageDescriptor(String imageName) {
         final Bundle pluginBundle =
              Platform.getBundle(HelmaPrototypeOutlineActivator.PLUGIN_ID);

         final Path imageFilePath =
              new Path(HelmaPrototypeOutlineActivator.ICON_BASE_URL + imageName);

         final URL imageFileUrl =
        	 FileLocator.find(pluginBundle, imageFilePath, null);
         return ImageDescriptor.createFromURL(imageFileUrl);
    }
    
    /**
     * Creates an image by loading it from a file in
     * the plugin's images directory.
     *
     * @param imageName The path to the image file to load.
     *     The image path must not include the plugin's images
     *     path location defined in <code>ICON_BASE_URL</code>.
     *     If you want to load <code>ICON_BASE_URL/myimage.gif</code>,
     *     simply pass <code>myimage.gif</code> to this method.
     *
     * @return The image object loaded from the image file
     */
    public static Image getImage(String imageName) {
         return getImageDescriptor(imageName).createImage();
    }

    /**
     * getter for viewer
     * @return the TreeView or null if the treeview hasn't been created yet
     */
	protected TreeViewer getViewer() {
		return viewer;
	}

	/**
	 * setter for viewer
	 * @param viewer the view used by the PrototypeOutline
	 */
	protected void setViewer(TreeViewer viewer) {
		this.viewer = viewer;
	}

}
